require 'aws'

class EC2
	include AWS

	ENDPOINT_URI = URI.parse(ENV['EC2_URL'])
	API_VERSION = '2008-08-08'
	SIGNATURE_VERSION = 1
	
	HTTP_METHOD = 'GET'

	def describe_images(options={})
		parameters = build_query_params(API_VERSION, SIGNATURE_VERSION, 
			{
			'Action' => 'DescribeImages',
			}, {
			'ImageId' => options[:image_ids],
			'Owner' => options[:owners],
			'ExecutableBy' => options[:executable_by]
			})
		
		response = do_query(HTTP_METHOD, ENDPOINT_URI, parameters)
		xml_doc = REXML::Document.new(response.body)
		
		images = []
		xml_doc.elements.each('//imagesSet/item') do |image|
			image_details = {
				:id => image.elements['imageId'].text,
				:location => image.elements['imageLocation'].text,
				:state => image.elements['imageState'].text,
				:owner_id => image.elements['imageOwnerId'].text,
				:is_public => image.elements['isPublic'].text == 'true'
			}

			image.elements.each('productCodes/item/productCode') do |code|
				image_details[:product_codes] ||= []
				image_details[:product_codes] << code.text
			end

			images << image_details
		end

		return images
	end
end
